//
//  ViewController.m
//  UI_14_ScrollerDelegate
//
//  Created by dllo on 16/3/28.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "ViewController.h"
#import "DelegateTableViewCell.h"
#import "DelegateModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,deleteDelegate>

@property (nonatomic, strong) NSArray *arrOfKey;
@property (nonatomic, strong) NSMutableDictionary *dicOfNew;
@property (nonatomic, strong) UITableView *tableview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self handleData];
}

-(void)creatView{
    
    _tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    _tableview.showsVerticalScrollIndicator = NO;
    
    [_tableview registerClass:[DelegateTableViewCell class] forCellReuseIdentifier:@"DelegateTableViewCell"];
    
    [self.view addSubview:_tableview];
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160101" ofType:@"plist"];
    
    NSDictionary *dicOfSource = [NSDictionary dictionaryWithContentsOfFile:path];
    
    
    self.dicOfNew = [NSMutableDictionary dictionary];
    
    //构建新的字典， 里面有model
    for (NSString *key in dicOfSource) {
        
        NSArray *arrOfContact = [dicOfSource objectForKey:key];
        
        
        NSMutableArray *arrOfNew = [NSMutableArray array];
        
        //遍历数组
        for (NSDictionary *dic in arrOfContact) {
            DelegateModel *model = [[DelegateModel alloc] init];
            
            //            model赋值(kvc)
            [model setValuesForKeysWithDictionary:dic];
            
            [arrOfNew addObject:model];
            
            
        }
        //        将存放model的数组存放到新的字典
        [self.dicOfNew setValue:arrOfNew forKey:key];
    }
    self.arrOfKey = [[self.dicOfNew allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.arrOfKey.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.arrOfKey objectAtIndex:section];
    NSArray *arr = [self.dicOfNew objectForKey:key];
    return  arr.count;    }


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DelegateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DelegateTableViewCell"];
    NSString *key = [self.arrOfKey objectAtIndex:indexPath.section];
    NSArray *arr = [self.dicOfNew objectForKey:key];
    
    
    DelegateModel *model = [arr objectAtIndex:indexPath.row];
    
    cell.delet = self;
    
    cell.model = model;
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.arrOfKey[section];
}

-(void)deletData:(DelegateTableViewCell *)sender{
    
    NSIndexPath *index = [self.tableview indexPathForCell:sender];
    NSString *key = self.arrOfKey[index.section];
    NSMutableArray *arr = [self.dicOfNew objectForKey:key];
    [arr removeObjectAtIndex:index.row];
//    if (arr[index.row] == 0) {
//        [self.arrOfKey reverseObjectEnumerator];
//    }
    [self.tableview deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableview reloadData];
    
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    
//    NSString *key = [self.arrOfKey objectAtIndex:indexPath.section];
//    NSMutableArray *arr = [self.dicOfNew objectForKey:key];
//    
//}


//-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//    return self.arrOfKey ;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
